﻿//Created  By 席吉波

(function() {

    var ScriptName = "LeftMenu";

    function execute() {

        Type.registerNamespace('Kevin.Controls');

        $LeftMenu = Kevin.Controls.LeftMenu = function(element) {
            Kevin.Controls.LeftMenu.initializeBase(this, [element]);
            this.CurrentLink = null;
            this.StateList = null;
            this.MenuList = null;
            this.Level1MenuList = null; //一级菜单
            this.Level2MenuOrAboveList = null; //二级及其以上菜单
            this._icoPosition = Kevin.Controls.IcoPosition.Left;
            this._menuStyle = Kevin.Controls.MenuStyle.Popup;
            this._target = document.body;
            this._cssClassName = "";
            this._popupMenuParent = null;
            this._ActiveSubMenuBounds = null;
            this._ActiveSubMenuClip = null;
            this._ActiveLevel2Menu = {};
            this._btnUpBounds = null;
            this._btnDownBounds = null;
            /*
            本控件所生成的div及其他html元素的命名规则
            //*/
            this.htmlElementIDFormat = { menuItem: "menuItem{0}"//菜单项
                               , menuItemContent: "menuItem{0}_Content"//菜单项中
                               , menuItemContentLeft: "menuItem{0}Content_left"
                               , menuItemContentCenter: "menuItem{0}Content_center"
                               , menuItemContentRight: "menuItem{0}Content_right"
                               , menuItemContentLink: "menuItem{0}Content_link"
                               , subMenuDiv: "menuItem{0}_sub"
            }

            this._popMenuHideTimer = null;
            this._tickHandler = null;
            this._popupMenuList = null; //当前已弹出的菜单窗体列表    
            //this._popupMenu = null;
            this._currentMenuItem = null; //当前已选中的菜单项

            this._defaultExpandMenuId = null; //默认展开的菜单项ID
            this._defaultExpandMenuIndex = 0; //默认展开的第一级菜单序列号

            this._menuItemNodes = null; //用于存储某一菜单项及其所有父节点,利用Array push pop的特性，可以自上而下的展示菜单
        }

        $LeftMenu.prototype = {
            initialize: function() {
                $LeftMenu.callBaseMethod(this, 'initialize');
                var menus = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField.value);
                this.Level1MenuList = new Array();
                this.Level2MenuOrAboveList = new Array();
                this._popupMenuList = new Array();

                this._menuItemNodes = new Array();
                this.MenuList = this.createMenu(null, this.get_element().id, menus.MenuList);
                menus.MenuList = this.MenuList;
                this._clientStateField.value = Sys.Serialization.JavaScriptSerializer.serialize(menus);
                //this.get_element().style.className=this._cssClassName;

                $common.addCssClasses(this.get_element(), [this._cssClassName]);

                this._tickHandler = Function.createDelegate(this, this._onTimerTick);
                this._popMenuHideTimer = new Sys.Timer();
                this._popMenuHideTimer.set_interval(500);
                this._popMenuHideTimer.add_tick(this._tickHandler);

                this._showDufaultMenuItem();

                $addHandler(window, "resize", Function.createDelegate(this, this._setSize));

            },
            dispose: function() {
                $LeftMenu.callBaseMethod(this, 'dispose');
                this.CurrentLink = null;
                this.StateList = null;
                this.MenuList = null;
            },

            createMenu: function(parentMenu, parentNodeID, menuList) {

                var parentNode = null;
                if (!(parentNodeID == null || parentNodeID == "")) {
                    parentNode = $get(parentNodeID);

                }

                for (var index in menuList) {

                    //这里还是输出菜单吧
                    //var menuItemID = "menuItem" + menuList[index].ID ;
                    var level = menuList[index].Level;

                    /*
                    var className="",itemClassName="";
                    if(level==1){
                    className = menuList[index].CssClass==null ? "cspbBtnCell":menuList[index].CssClass ;
                    //itemClassName =
                    }
                    else{
                    className = menuList[index].CssClass==null ? "cspbItm":menuList[index].CssClass;
                
            }
                    //*/

                    parentNode = $get(parentNodeID);

                    var menuItem = this.createMenuItem(parentNode, menuList[index]);
                    menuList[index].ParentMenu = parentMenu;
                    menuList[index].MenuID = menuItem.id;

                    if (level == 1) {
                        $common.removeCssClasses(menuItem, ["menuItem"]);
                        $common.addCssClasses(menuItem, ["mainItem"]);
                        this.Level1MenuList.push(menuItem);
                    }

                    if (menuList[index].HasChildMenu === true) {

                        var subDivID = "menuItem" + menuList[index].ID + "_Sub";

                        menuList[index].SubMenuID = subDivID;

                        var paras = { ParentMenuID: menuItem.id, SubID: subDivID, Level: menuList[index].Level };

                        this.Level2MenuOrAboveList.push(paras);

                        if (level >= 2 && this._menuStyle == Kevin.Controls.MenuStyle.Popup) {

                            parentNode = document.body;
                        }


                        if ($get(subDivID) == null) {
                            $common.createElementFromTemplate({
                                nodeName: "div",
                                properties: {
                                    id: subDivID,
                                    style: {
                                        display: "none"
                                    }
                                }
                            }, parentNode);
                        }

                        //*
                        if (level == 1) {

                            $addHandler(menuItem, "click", Function.createDelegate(this, Function.createCallback(this.showSubMenu, paras)));

                        }
                        else {

                            $common.addCssClasses($get(subDivID), ["popupMenu"]);
                            //菜单项的控制
                            $addHandler(menuItem, "mouseover", Function.createDelegate(this, Function.createCallback(this.menuItemMouseover, paras)));
                            $addHandler(menuItem, "mouseout", Function.createDelegate(this, Function.createCallback(this.menuItemMouseout, paras)));
                            //menuItemSetCurrentCSS

                            //弹出菜单的显示/隐藏控制
                            $addHandler($get(subDivID), "mouseover", Function.createDelegate(this, function() { this._popMenuHideTimer.set_enabled(false); }));
                            $addHandler($get(subDivID), "mouseout", Function.createDelegate(this, function() { this._popMenuHideTimer.set_enabled(true); }));

                        }
                        //*/

                        //迭代生成 2+ 级菜单
                        var currentMenuItem = { "id": menuItem.id, "popDivID": subDivID, "Text": menuList[index].Text, "Link": menuList[index].Link };
                        this.createMenu(currentMenuItem, subDivID, menuList[index].ChildMenu);
                        //arguments.callee(currentMenuItem, subDivID, menuList[index].ChildMenu);
                    }
                    else {
                        //parentMenuID = null;

                        var paras = { ParentMenuID: menuItem.id, SubID: "", Level: menuList[index].Level };

                        this.Level2MenuOrAboveList.push(paras);
                        $addHandler(menuItem, "mouseover", Function.createDelegate(this, Function.createCallback(this.menuItemMouseover, paras)));
                        $addHandler(menuItem, "mouseout", Function.createDelegate(this, Function.createCallback(this.menuItemMouseout, paras)));
                        //$addHandler(menuItem,"click",Function.createDelegate(this,Function.createCallback(this.showSubMenu,paras)));
                    }

                } // end for function
                return menuList;
            },
            createMenuItem: function(parentNode, menuItem) {
                if (this._icoPosition == Kevin.Controls.IcoPosition.Left)
                    return this.createMenuItem1(parentNode, menuItem);
                else
                    return this.createMenuItem2(parentNode, menuItem);
            },
            createMenuItem1: function(parentNode, menuItem) {
                ///<summary>创建菜单项</summary>
                ///<parameters name='parentNode'>父节点<parameters>
                ///<parameters name='menuItem'>菜单项的Json对象<parameters>
                /*
                1：图标在左
                ┌─────────────────────┐
                │┌────┐┌───────┐ ┌───┐ │
                ││        ││              │ │      │ │
                ││        ││              │ │      │ │
                ││        ││              │ │      │ │
                │└────┘└───────┘ └───┘ │
                │┌───────────────────┐│
                │└───────────────────┘│
                └─────────────────────┘
                */
                var menuItemID = "menuItem_" + menuItem.ID;
                var itemDiv = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID
                    },
                    cssClasses: ["menuItem"]
                }, parentNode);

                var itemContent = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "_content"
                    },
                    cssClasses: ["itemContent"]
                }, itemDiv);

                var itemDivLeft = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "Left"
                    },
                    cssClasses: ["itemIndexIco"]
                }, itemContent);
                if (menuItem.Image != null) {
                    $common.addCssClasses(itemDivLeft, [menuItem.Image]);
                }
                var itemDivCenter = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "Center"
                    },
                    cssClasses: ["itemCenter"]

                }, itemContent);
                if (menuItem.Link != "" && menuItem.Link != null) {
                    //itemDivCenter.innerHTML="<a href='"+menuItem.Link+"' target='"+this._target+"'>"+menuItem.Text+"</a>";
                    var item = $common.createElementFromTemplate({
                        nodeName: "a",
                        properties: {
                            id: menuItemID + "Link",
                            href: menuItem.Link,
                            target: this._target
                        },
                        cssClasses: ["link"]
                    }, itemDivCenter);
                    item.innerHTML = menuItem.Text;
                    var para = { "id": item.id, "from": "internal" };
                    $addHandler(item, "click", Function.createDelegate(this, Function.createCallback(this.menuItemSetCurrentCSS, para)));
                }
                else {
                    itemDivCenter.innerHTML = menuItem.Text;
                    var para = { "id": menuItemID + "Link", "from": "internal" };
                    //$addHandler(menuItem,"click", Function.createDelegate(this,Function.createCallback(this.menuItemSetCurrentCSS,para)));
                    $addHandler(itemContent, "click", Function.createDelegate(this, function() { eval(menuItem.Script); this.menuItemSetCurrentCSS(null, para); }));
                }
                //如果菜单项有子菜单，则此需有一个提示图标，并给itemDiv绑定相应的事件
                var itemDivRight = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "Right"
                    }
                }, itemContent);
                if (menuItem.HasChildMenu == true) {
                    //itemDivRight.style.className='';
                    $common.addCssClasses(itemDivRight, ["itemSubMenuIco"]);
                    //$addHandler(itemDiv,"mouseOver","menuItemMouseOver function");
                }
                /*
                var itemDivBottom= $common.createElementFromTemplate({
                nodeName:"div",
                properties:{
                id:menuItemID+"Bottom"
                },
                cssClasses:["itemBottom"]
                },itemDiv);  
                //*/
                return itemDiv;
            },
            ///<summary>创建菜单项</summary>
            ///<parameters name='parentNode'>父节点<parameters>
            ///<parameters name='menuItem'>菜单项的Json对象<parameters>
            createMenuItem2: function(parentNode, menuItem) {
                /*
                生成如下格式的菜单选项       
            
         2：图标在上               
                ┌─────────────────────┐
                │ ┌──────────────────┐ │
                │ │                                    │ │
                │ │                                    │ │
                │ │                                    │ │
                │ └──────────────────┘ │
                │┌─────────────┐ ┌───┐ │
                ││                          │ │      │ │
                ││                          │ │      │ │
                ││                          │ │      │ │
                │└─────────────┘ └───┘ │
                └─────────────────────┘
                */

                var menuItemID = "menuItem_" + menuItem.ID;
                var itemDiv = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID
                    },
                    cssClasses: ["menuItem"]
                }, parentNode);

                var itemContent = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "_content"
                    },
                    cssClasses: ["itemContent"]
                }, itemDiv);

                var itemDivTop = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "Top"
                    },
                    cssClasses: ["itemIndexIco"]
                }, itemContent);

                var itemDivBottomLeft = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "Center"
                    },
                    cssClasses: ["itemBottomLeft"]

                }, itemContent);

                if (menuItem.Link != "") {
                    //itemDivCenter.innerHTML="<a href='"+menuItem.Link+"' target='"+this._target+"'>"+menuItem.Text+"</a>";
                    var item = $common.createElementFromTemplate({
                        nodeName: "a",
                        properties: {
                            id: menuItemID + "Link",
                            href: menuItem.Link,
                            target: this._target
                        },
                        cssClasses: ["link"]
                    }, itemDivCenter);
                    item.innerHTML = menuItem.Text;
                    var para = { "id": item.id, "from": "internal" };
                    $addHandler(item, "click", Function.createDelegate(this, Function.createCallback(this.menuItemSetCurrentCSS, para)));
                }
                else {
                    itemDivCenter.innerHTML = menuItem.Text;
                    var para = { "id": menuItemID + "Link", "from": "internal" };
                    $addHandler(itemDivCenter, "click", Function.createDelegate(this, Function.createCallback(this.menuItemSetCurrentCSS, para)));
                }
                //如果菜单项有子菜单，则此需有一个提示图标，
                var itemBottomRight = $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: menuItemID + "Right"
                    }
                }, itemContent);
                if (menuItem.HasChildMenu == true) {

                    $common.addCssClasses(itemBottomRight, ["itemSubMenuIco"]);

                }

                return itemDiv;
            },
            showSubMenu: function(evt, context) {
                //对于第一级的菜单需点击后展开菜单
                //var mainID= context.ID;

                var subMenuMainDivID = context.SubID;
                var level = context.Level;


                if (subMenuMainDivID == "") {
                    return;
                }

                //$common.addCssClasses($get(context.ParentMenuID),["hover"]);

                //然后显示当前2+级菜单
                if (level == 1 || this._menuStyle == Kevin.Controls.MenuStyle.Inner) {
                    this.expandMenu(evt, context);
                }
                else {
                    this.popUpMenu(evt, context);
                }
            },
            expandMenu: function(evt, context) {
                //对于第一级的菜单需点击后展开菜单,
                //var parentMenuID= context.ID;
                //-----------------------------------------------------------------
                //这里是因为SetTimeOut函数在IE 和Firefox下传入的参数不同所致
                //这个算是用Hanck的方法来解决吧
                if ((context == "undefined" || context == null) && typeof (evt) == "object") {
                    context = evt;
                }
                //-----------------------------------------------------------------
                var subMenuMainDivID = context.SubID;
                var level = context.Level;
                var menuItemWidth = $common.getBounds(this.get_element()).width - 5;
                var menuItemHeight = $common.getBounds(this.Level1MenuList[0]).height;

                if (subMenuMainDivID == "") {
                    return;
                }
                var subMenuMainDiv = $get(subMenuMainDivID);
                //先把其他已显示的 2+ 级菜单隐藏

                for (var index in this.Level2MenuOrAboveList) {

                    var subID = this.Level2MenuOrAboveList[index].SubID;
                    var subMenu = $get(subID);
                    if (subMenu != null) {
                        $common.setVisible(subMenu, false);
                    }
                }
                //subMenuMainDiv.style.display = subMenuMainDiv.style.display =="block"?"none":"block";
                //在这里处理Scollbar
                var winHeight = this.get_element().clientHeight;
                var level1MenusHeight = 0;

                for (var index in this.Level1MenuList) {
                    level1MenusHeight += $common.getBounds(this.Level1MenuList[index]).height;
                }

                var temp = false;
                var offsetY = 0;
                var subMenuDivHeight = 0;
                for (var index in this.Level1MenuList) {

                    //var bound=$common.getBounds(this.Level1MenuList[index]);
                    offsetY = (parseInt(index) * menuItemHeight) + subMenuDivHeight;

                    $common.setLocation(this.Level1MenuList[index], new Sys.UI.Bounds(0, offsetY, menuItemWidth, menuItemHeight));
                    this.Level1MenuList[index].style.width = menuItemWidth;
                    if (this.Level1MenuList[index].id == context.ParentMenuID) {

                        var subMenuDivBounds = new Sys.UI.Bounds(0, offsetY + menuItemHeight, menuItemWidth, menuItemHeight);
                        $common.setLocation(subMenuMainDiv, subMenuDivBounds);
                        $common.setVisible(subMenuMainDiv, true);
                        var currentSubMenuBound = $common.getBounds(subMenuMainDiv);
                        var diff = winHeight - level1MenusHeight - currentSubMenuBound.height;
                        //subMenuDivHeight = diff >= 0 ? $common.getBounds(subMenuMainDiv).height : winHeight - level1MenusHeight;
                        //为了是2级菜单占据整个空白区域，
                        subMenuDivHeight = winHeight - level1MenusHeight;
                        subMenuMainDiv.style.width = menuItemWidth;

                        if (diff < 0) {
                            subMenuMainDiv.style.clip = String.format("rect(0 {0} {1} 0)", menuItemWidth, subMenuDivHeight);
                            context.Height = $common.getBounds(subMenuMainDiv).height;
                            context.VisibleHeight = subMenuDivHeight;
                            context.Bounds = subMenuDivBounds;
                            context.OffsetY = offsetY;
                            this._ActiveSubMenuBounds = new Sys.UI.Bounds(subMenuDivBounds.x, subMenuDivBounds.y, subMenuDivBounds.width, subMenuDivHeight);
                            context.Step = menuItemHeight;
                            this._ActiveSubMenuClip = { "Top": 0, "Right": menuItemWidth, "Bottom": subMenuDivHeight, "Left": 0 };
                            this.doUpDownEvent(evt, context);
                        }
                        else {
                            //subMenuMainDiv.style.clip ="";
                            this._ActiveSubMenuClip = { "Top": 0, "Right": menuItemWidth, "Bottom": subMenuDivHeight, "Left": 0 };
                            this.removeUpDownEvent(evt, context);
                        }
                    }
                }
                this._ActiveLevel2Menu = context;
            },
            popUpMenu: function(evt, context) {

                var parentMenuID = context.ParentMenuID;
                var subMenuMainDivID = context.SubID;

                if (subMenuMainDivID == "") {
                    return;
                }

                var subMenuMainDiv = $get(subMenuMainDivID);

                if (this._menuStyle == Kevin.Controls.MenuStyle.Inner) {
                    subMenuMainDiv.style.display = subMenuMainDiv.style.display == "block" ? "none" : "block";
                    return;
                }
                var parentMenuItem = $get(parentMenuID);
                var x = parentMenuItem.offsetLeft;
                var y = parentMenuItem.offsetTop;
                var parentBounds = $common.getBounds(parentMenuItem);

                //如果弹出的菜单超过屏幕下方的，则位置需向上移动
                $common.setVisible(subMenuMainDiv, true);
                $common.setLocation(subMenuMainDiv, new Sys.UI.Bounds(0, 0, 40, 20));
                var height = subMenuMainDiv.clientHeight;
                var offsetY = 0;
                var elmentBounds = $common.getBounds(this.get_element());
                var elmentBottom = elmentBounds.y + elmentBounds.height;
                if (height + parentBounds.y > elmentBottom) {
                    offsetY = height + parentBounds.y - elmentBottom;
                }
                $common.setVisible(subMenuMainDiv, false);
                var bounds = new Sys.UI.Bounds(parentBounds.x + parentBounds.width - 2, parentBounds.y - offsetY, 40, height);
                $common.setLocation(subMenuMainDiv, bounds);
                $common.setVisible(subMenuMainDiv, true);

                //this._popupMenu = subMenuMainDivID;

                for (var index in this._popupMenuList) {
                    if (this._popupMenuList[index].id != subMenuMainDivID && context.Level <= this._popupMenuList[index].level) {
                        $common.setVisible($get(this._popupMenuList[index].id), false);
                    }
                }

                //this._popupMenuList = new Array();
                var contains = false;
                for (var index in this._popupMenuList) {
                    if (this._popupMenuList[index].id == subMenuMainDivID) {
                        contains = true;
                        break;
                    }
                }

                if (!contains) {
                    this._popupMenuList.push({ "id": subMenuMainDivID, "level": context.Level });
                }

                this._popMenuHideTimer.set_enabled(false);
                //在这里处理Scollbar        
            },
            createUpDownBtn: function(evt, context) {

                if ($get("btnMenuUp")) {
                    //$remove("btnMenuUp");
                    //$remove("btnMenuDown");
                    $common.removeElement($get("btnMenuUp"));
                    $common.removeElement($get("btnMenuDown"));

                }

                var parentNode = $get(context.SubID);

                $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: "btnMenuUp"

                    },
                    cssClasses: ["btnMenuUp"]
                }, parentNode);

                $common.createElementFromTemplate({
                    nodeName: "div",
                    properties: {
                        id: "btnMenuDown"
                    },
                    cssClasses: ["btnMenuDown"]
                }, parentNode);

            },
            removeUpDownEvent: function(evt, context) {
                if ($get("btnMenuUp")) {
                    $common.removeElement($get("btnMenuUp"));
                    $common.removeElement($get("btnMenuDown"));
                }
                if (typeof ($get(context.SubID).onmousewheel) === 'undefined') {
                    //$addHandler($get(context.SubID), 'DOMMouseScroll', Function.createDelegate(this,Function.createCallback(this.subMenuDivMouseWheel,context)));
                    //$addHandler(document, 'DOMMouseScroll', this._documentMouseWheelHandler);
                    //$removeHandler($get(context.SubID),'DOMMouseScroll',Function.createDelegate(this,Function.createCallback(this.subMenuDivMouseWheel,context))['DOMMouseScroll']);
                }
                else {
                    //$addHandler($get(context.SubID), 'mousewheel', Function.createDelegate(this,Function.createCallback(this.subMenuDivMouseWheel,context)));
                    //$addHandler(document, 'mousewheel', this._documentMouseWheelHandler);
                    //$removeHandler($get(context.SubID),'mousewheel',Function.createDelegate(this,Function.createCallback(this.subMenuDivMouseWheel,context))['mousewheel']);
                }
            },
            doUpDownEvent: function(evt, context) {
                //创建上下按钮并绑定事件 在绑定事件前应Remove其事件
                this.createUpDownBtn(evt, context);

                var btnUp = $get("btnMenuUp");
                var btnDown = $get("btnMenuDown");
                var bound = this._ActiveSubMenuBounds;

                //btnUp.innerHTML="↑";
                //btnDown.innerHTML="↓";

                $addHandler(btnUp, "click", Function.createDelegate(this, Function.createCallback(this.menuScollUp, context)));
                $addHandler(btnDown, "click", Function.createDelegate(this, Function.createCallback(this.menuScollDown, context)));

                var boundUp = { "x": bound.x, "y": bound.y, "width": bound.width, "height": bound.height };
                boundUp.x = parseInt(bound.x + bound.width - btnUp.clientWidth);
                boundUp.y = 0; //parseInt(boundUp.y-btnUp.clientHeight);

                var boudDown = { "x": boundUp.x, "y": boundUp.y, "width": boundUp.width, "height": boundUp.height };
                boudDown.y = parseInt(context.VisibleHeight - btnDown.clientHeight); //parseInt( bound.y+context.VisibleHeight-(2*btnDown.clientHeight));

                $common.setLocation(btnUp, boundUp);
                $common.setLocation(btnDown, boudDown);
                //在这里保存 上、下滚动按钮的物理位置
                this._btnUpBounds = { "x": boundUp.x, "y": boundUp.y, "width": boundUp.width, "height": boundUp.height }; ;
                this._btnDownBounds = { "x": boudDown.x, "y": boudDown.y, "width": boudDown.width, "height": boudDown.height }; ;

                $common.setVisible(btnUp, false);

                //滚轮事件
                if (typeof ($get(context.SubID).onmousewheel) === 'undefined') {
                    $addHandler($get(context.SubID), 'DOMMouseScroll', Function.createDelegate(this, Function.createCallback(this.subMenuDivMouseWheel, context)));
                    //$addHandler(document, 'DOMMouseScroll', this._documentMouseWheelHandler);
                }
                else {
                    $addHandler($get(context.SubID), 'mousewheel', Function.createDelegate(this, Function.createCallback(this.subMenuDivMouseWheel, context)));
                    //$addHandler(document, 'mousewheel', this._documentMouseWheelHandler);
                }
            },
            subMenuDivMouseWheel: function(e, context) {
                var direction;
                if (typeof (e.rawEvent.wheelDelta) === 'undefined') {
                    // firefox
                    direction = (e.rawEvent.detail >= 1) ? 1 : -1;
                }
                else {
                    // not firefox
                    direction = (e.rawEvent.wheelDelta > 1) ? -1 : 1;
                }
                if (direction > 0) {
                    this.menuScollDown(e, context);
                }
                else {
                    this.menuScollUp(e, context);
                }

                e.stopPropagation();
                e.preventDefault();
                return false;

            },
            menuScollUp: function(evt, context) {
                //子菜单向上滚动
                if (this._ActiveSubMenuClip.Top == 0) {
                    return;
                }
                var menuDiv = $get(context.SubID);
                var bound = this._ActiveSubMenuBounds;

                bound.y = bound.y + context.Step;

                //设置活动子菜单的Clip属性
                this._ActiveSubMenuClip.Top = this._ActiveSubMenuClip.Top - context.Step < 0 ? 0 : this._ActiveSubMenuClip.Top - context.Step;
                this._ActiveSubMenuClip.Bottom = this._ActiveSubMenuClip.Bottom - context.Step < context.VisibleHeight ? context.VisibleHeight : this._ActiveSubMenuClip.Bottom - context.Step;

                menuDiv.style.clip = String.format("rect({0} {1} {2} {3})"
                                            , this._ActiveSubMenuClip.Top
                                            , this._ActiveSubMenuClip.Right
                                            , this._ActiveSubMenuClip.Bottom
                                            , this._ActiveSubMenuClip.Left);

                $common.setLocation(menuDiv, bound);
                //这里判断是否隐藏自己并显示向下滚动按钮              

                $common.setVisible($get("btnMenuDown"), true);

                this._btnUpBounds.y = this._btnUpBounds.y - context.Step;
                this._btnDownBounds.y = this._btnDownBounds.y - context.Step;

                $common.setLocation($get("btnMenuUp"), this._btnUpBounds);
                $common.setLocation($get("btnMenuDown"), this._btnDownBounds);

                if (this._ActiveSubMenuClip.Top == 0) {
                    //为修正误差，在滚动至菜单顶部时需重新调整位置
                    this._ActiveSubMenuBounds.y = context.OffsetY + context.Step; ;
                    this._btnUpBounds.y = 0; ;
                    $common.setLocation(menuDiv, this._ActiveSubMenuBounds);
                    $common.setLocation(btnMenuUp, this._btnUpBounds);

                    $common.setVisible($get("btnMenuUp"), false);

                }
            },
            menuScollDown: function(evt, context) {
                //子菜单向下滚动
                if (this._ActiveSubMenuClip.Bottom == context.Height) {
                    return;
                }
                var menuDiv = $get(context.SubID);
                var bound = this._ActiveSubMenuBounds;

                bound.y = Math.abs(bound.y - context.Step - context.VisibleHeight) >= context.Height ? context.VisibleHeight - context.Height : (bound.y - context.Step);
                //设置活动子菜单的Clip属性
                this._ActiveSubMenuClip.Top = this._ActiveSubMenuClip.Top + context.Step + context.VisibleHeight > context.Height ? context.Height - context.VisibleHeight : this._ActiveSubMenuClip.Top + context.Step;
                this._ActiveSubMenuClip.Bottom = this._ActiveSubMenuClip.Bottom + context.Step > context.Height ? context.Height : this._ActiveSubMenuClip.Bottom + context.Step;

                menuDiv.style.clip = String.format("rect({0} {1} {2} {3})"
                                            , this._ActiveSubMenuClip.Top
                                            , this._ActiveSubMenuClip.Right
                                            , this._ActiveSubMenuClip.Bottom
                                            , this._ActiveSubMenuClip.Left);

                $common.setLocation(menuDiv, bound);
                //这里判断是否隐藏自己并显示向上滚动按钮

                this._btnDownBounds.y = this._btnDownBounds.y + context.Step;
                this._btnUpBounds.y = this._btnUpBounds.y + context.Step;

                $common.setLocation($get("btnMenuUp"), this._btnUpBounds);
                $common.setLocation($get("btnMenuDown"), this._btnDownBounds);
                $common.setVisible($get("btnMenuUp"), true);

                if (this._ActiveSubMenuClip.Bottom == context.Height) {
                    //当滚动至尾部时，需要调节按钮和菜单的位置  
                    //var menuDivBounds =   new Sys.UI.Bounds(this._ActiveSubMenuBounds.x,context.VisibleHeight-context.Height+context.OffsetY+context.Step,this._ActiveSubMenuBounds.width,this._ActiveSubMenuBounds.height);  
                    this._ActiveSubMenuBounds.y = context.VisibleHeight - context.Height + context.OffsetY + context.Step;
                    $common.setLocation(menuDiv, this._ActiveSubMenuBounds);
                    //this._ActiveSubMenuBounds = menuDivBounds;

                    //var btnDownBounds = new Sys.UI.Bounds(this._btnDownBounds.x,this._btnUpBounds.y+context.VisibleHeight-context.Step,this._btnDownBounds.width,this._btnDownBounds.height);
                    this._btnDownBounds.y = this._btnUpBounds.y + context.VisibleHeight - context.Step;
                    $common.setLocation($get("btnMenuDown"), this._btnDownBounds);
                    //this._btnDownBounds = btnDownBounds;
                    $common.setVisible($get("btnMenuDown"), false);
                }

            },
            _scrollMenuTo: function(context) {
                if (context.menuItemID && $get(context.menuItemID)) {
                    var bounds = $common.getBounds($get(context.menuItemID));
                    //if(bounds.y)
                }
            },
            hiddenSubMenu: function(evt, context) {
                $common.setVisible($get(context.SubID), false);
            },
            menuItemMouseout: function(e, context) {
                $common.removeCssClasses($get(context.ParentMenuID), ["hover"]);
                this._popMenuHideTimer.set_enabled(true);
            },
            menuItemMouseover: function(e, context) {
                $common.addCssClasses($get(context.ParentMenuID), ["hover"]);
                //*
                for (var index in this._popupMenuList) {
                    if (context.Level <= this._popupMenuList[index].level) {
                        $common.setVisible($get(this._popupMenuList[index].id), false);
                    }
                }
                if (context.SubID) {

                    this.showSubMenu(e, context);
                }

                //*/
            },
            menuItemSetCurrentCSS: function(e, context) {

                var elt = $get(context.id);
                if (elt == null) return;

                //先移除当前已选的菜单项
                this.menuItemRemoveCurrentCSS();

                if (context.from == 'internal') {
                    this._currentMenuItem = context;

                    $common.addCssClasses(elt, ["current"]);
                    $common.removeCssClasses($get(this._currentMenuItem.id), ["link"]);

                }
                else {
                    //说明是从外部的Javascript来操作的，此时需要将其parentNode展开 并需显示在适当的坐标上
                    var linkID = context.id + "Link";
                    this._currentMenuItem = { "id": linkID, "from": "" };

                    $common.addCssClasses($get(linkID), ["current"]);
                    $common.removeCssClasses($get(this._currentMenuItem.id), ["link"]);

                    this._menuItemNodes = new Array();
                    var temp = this.findMenuItem(context.id, this.MenuList);
                    for (var index in this._menuItemNodes) {
                        //alert(this._menuItemNodes[index]);
                        //alert(this._menuItemNodes[index].Text);
                        if (this._menuItemNodes[index].HasChildMenu) {
                            this.showSubMenu(this, {
                                ParentMenuID: this._menuItemNodes[index].MenuID
                                            , SubID: this._menuItemNodes[index].SubMenuID
                                            , Level: this._menuItemNodes[index].Level
                            }
                                    );
                        }
                        this._popMenuHideTimer.set_enabled(true);
                    }
                }
            },
            findMenuItem: function(menuItemID, MenuList) {
                //根据子菜单项查找其完整路径
                for (var index in MenuList) {

                    this._menuItemNodes.push(MenuList[index]);

                    if (MenuList[index].HasChildMenu == true) {
                        this.findMenuItem(menuItemID, MenuList[index].ChildMenu);
                    }
                    if (MenuList[index].MenuID == menuItemID) {
                        this._menuItemNodes.push(MenuList[index]);
                        break;
                    }
                    else {
                        this._menuItemNodes.pop(MenuList[index]);
                    }

                }

            },
            menuItemRemoveCurrentCSS: function() {
                //移除当前已选的菜单项的样式
                if (this._currentMenuItem == null) {
                    return;
                }
                var elt = $get(this._currentMenuItem.id);
                if (elt == null) return;

                $common.addCssClasses(elt, ["link"]);
                $common.removeCssClasses(elt, ["current"]);
            },
            _onTimerTick: function() {
                for (var index in this._popupMenuList) {
                    $common.setVisible($get(this._popupMenuList[index].id), false);
                }
            },
            _setSize: function() {
                //其实这里应该重新设定_ActiveLevel2Menu中SubID的可见区域，这里属于偷懒的做法
                var menuVisible = $common.getVisible(this.get_element());
                if (!menuVisible) {
                    return;
                }
                setTimeout(Function.createDelegate(this, Function.createCallback(this.expandMenu, this._ActiveLevel2Menu)), 100);

                //this.expandMenu(null,this._ActiveLevel2Menu); 

            },
            _showDufaultMenuItem: function() {
                if (this._defaultExpandMenuId && $get("menuItem_" + this._defaultExpandMenuId)) {
                    this.menuItemSetCurrentCSS(null, { id: "menuItem_" + this._defaultExpandMenuId, from: "" });
                    return;
                }

                for (var index in this.MenuList) {
                    if (index == this._defaultExpandMenuIndex) {
                        this.expandMenu(null, { ParentMenuID: "menuItem_" + this.MenuList[index].ID, SubID: "menuItem" + this.MenuList[index].ID + "_Sub", Level: this.MenuList[index].Level });
                        break;
                    }
                }

            },
            addEvent: function() {
                if (!this.MenuList) {
                    this.StateList = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField.value);
                    this.MenuList = this.StateList.MenuList;
                    this.CurrentLink = this.StateList.CurrentLink;
                }
                for (var i = 0; i < this.MenuList.length; i++) {
                    var controlID = "menu" + this.MenuList[i].ID;
                    var control = $get(controlID);
                    if (control) {
                        $addHandler(control, 'click', Function.createDelegate(this, this.setCurrentLink));
                    }
                }
            },
            setCurrentLink: function(obj) {
                for (var i = 0; i < this.MenuList.length; i++) {
                    var controlID = "menu" + this.MenuList[i].ID;
                    if (obj.target.id == controlID) {
                        this.CurrentLink = obj.target.id;
                    }
                }
                var v_state = { 'CurrentLink': this.CurrentLink, 'MenuList': this.MenuList }
                this._clientStateField.value = Sys.Serialization.JavaScriptSerializer.serialize(v_state);
                this.saveClientState();
                return true;
            },
            loadClientState: function() {
                if (this._clientStateField.value == '' || this._clientStateField.value == 'undefined') {
                    return;
                }
                this.StateList = Sys.Serialization.JavaScriptSerializer.deserialize(this._clientStateField);
            },
            saveClientState: function() {
                return Sys.Serialization.JavaScriptSerializer.serialize(this._clientStateField.value);
            },
            get_clientStateField: function() {
                return this._clientStateField;
            },
            set_clientStateField: function(value) {
                if (this._clientStateField != value) {
                    this._clientStateField = value;
                    this.raisePropertyChanged('clientStateField');
                }
            },
            get_target: function() {
                return this._target;
            },
            set_target: function(value) {
                this._target = value;
                this.raisePropertyChanged('target');
            },
            get_iconPosetion: function() {
                return this._iconPosetion;
            },
            set_iconPosetion: function(value) {
                this._iconPosetion = value;
                this.raisePropertyChanged("iconPosetion");
            },
            get_menuStyle: function() {
                return this._menuStyle;
            },
            set_menuStyle: function(value) {
                this._menuStyle = value;
                this.raisePropertyChanged("menuStyle");
            },
            get_cssClassName: function() {
                return this._cssClassName;
            },
            set_cssClassName: function(value) {
                this._cssClassName = value;
                this.raisePropertyChanged("cssClassName");
            },
            get_popupMenuParent: function() {
                return this._popupMenuParent;
            },
            set_popupMenuParent: function(value) {
                this._popupMenuParent = value;
                this.raisePropertyChanged("popupMenuParent");
            },
            get_defaultExpandMenuId: function() {
                return this._defaultExpandMenuId;
            },
            set_defaultExpandMenuId: function(value) {
                this._defaultExpandMenuId = value;
            },
            get_defaultExpandMenuIndex: function() {
                return this._defaultExpandMenuIndex;
            },
            set_defaultExpandMenuIndex: function(value) {
                this._defaultExpandMenuIndex = value;
            }

        }

        $LeftMenu.registerClass('Kevin.Controls.LeftMenu', Sys.UI.Control);

        //枚举 ： 图标的放置位置
        Kevin.Controls.IcoPosition = function() {
            throw Error.invalidOperation();
        }
        Kevin.Controls.IcoPosition.prototype = {
            Left: 0,
            Top: 1
        }
        Kevin.Controls.IcoPosition.registerEnum("Kevin.Controls.IcoPosition", false)

        //枚举 ： 2+级菜单的弹出样式
        Kevin.Controls.MenuStyle = function() {
            throw Error.invalidOperation();
        }
        Kevin.Controls.MenuStyle.prototype = {
            Popup: 0,
            Inner: 1
        }
        Kevin.Controls.MenuStyle.registerEnum("Kevin.Controls.MenuStyle", false)


        if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
    } //execute
    if (window.Sys && Sys.loader) {
        Sys.loader.registerScript(ScriptName, ["ExtendedModalPopup"], null, execute);

    }
    else {
        execute();
    }
})();